import {
  asyncRoutes,
  constantRoutes
} from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = {
      ...route
    }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

export function pdRoles(routes, roles, path) {
  var pdName = []
  var res = []
  var activeList = []
  var activeMenu = ''
  var zhixiang = false
  routes.forEach((route, index) => {
    const tmp = {
      ...route
    }
    pdName = []
    var biaoshi = 0
    roles.forEach((role, index1) => {
      if (index === 0) {
        if (tmp.path === role && pdName.indexOf(role) === -1) {
          pdName.push(role)
          res.push(tmp)
          biaoshi = 1
        }
        if (index1 === roles.length - 1 && biaoshi === 0) {
          zhixiang = true
        }
      } else {
        if (tmp.path === role && pdName.indexOf(role) === -1) {
          if (zhixiang) {
            zhixiang = false
            activeList.push({
              yuan: tmp.meta.activeMenu,
              xian: path === '/' ? '/' + role : path + '/' + role
            })
            tmp.hidden = false
          }
          pdName.push(role)
          res.push(tmp)
        }
      }
    })
  })

  for (var i = 0; i < res.length; i++) {
    activeList.forEach(active => {
      const act = {
        ...active
      }
      if (res[i].meta.activeMenu === act.yuan) {
        res[i].meta.activeMenu = act.xian
      }
    })
  }
  return res.length > 0 ? res : null
}

export function pdRoutes(routes, roles) {
  var res = []
  var row = constantRoutes.concat(routes)
  row.forEach(route => {
    const tmp = {
      ...route
    }
    if (!tmp.hidden) {
      // console.log(roles)
      var children = pdRoles(tmp.children, roles, tmp.path)
      if (children) {
        tmp.children = children
        res.push(tmp)
      }
    } else {
      res.push(tmp)
    }
  })
  return res
}

const state = {
  routes: [],
  routes1: [],
  addRoutes: [],
  redirect: '/'
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes.accessedRoutes
    // state.routes = constantRoutes.concat(routes.accessedRoutes)
    state.routes = pdRoutes(routes.accessedRoutes, routes.roles)
    // state.routes = pdRoutes(routes.accessedRoutes,["workbenchRemind"])
    state.routes1 = pdRoutes(routes.accessedRoutes, ['brandManage2', 'liansuo', 'liansuo2', 'journal', 'currencycard', 'Finance', 'integralset', 'integralsetedit', 'userauth', 'memberlist2', 'shareholder2', 'level', 'share', 'redar', 'coupon', 'cardbag', 'smsTopUp2', 'smsBuy2', 'smsSendRecord2', 'distribute', 'distributeLog'])
  },
  SET_REDIRECT: (state, routes) => {
    state.redirect = routes.redirect
  }
}

const actions = {
  generateRoutes({
    commit
  }, roles) {
    return new Promise(resolve => {
      const accessedRoutes = asyncRoutes
      commit('SET_ROUTES', {
        'accessedRoutes': accessedRoutes,
        'roles': roles
      })
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
